home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tclX6.4c / dist / tests / flock.test < prev    next >
Encoding:
Text File  |  1992-11-07  |  5.9 KB  |  230 lines

  1. #
  2. # filecmds.test
  3. #
  4. # Tests for the flock and funlock commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: flock.test,v 2.0 1992/10/16 04:49:43 markd Rel $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. catch {flock} msg
  22. if {$msg == "File locking is not available on this system"} return
  23.  
  24. #
  25. # Create and open a read file and a write file.
  26. #
  27. unlink -nocomplain {FLOCKR.TMP FLOCKW.TMP}
  28.  
  29. foreach X {R W RW} {
  30.     set fh [open FLOCK${X}.TMP w]
  31.     puts $fh [replicate X 100]
  32.     close $fh
  33. }
  34.  
  35. set readFH  [open FLOCKR.TMP r]
  36. set writeFH [open FLOCKW.TMP w]
  37. set rdwrFH  [open FLOCKRW.TMP r+]
  38.  
  39. #
  40. # Check flock argument checking
  41. #
  42.  
  43. set flockWrongArgs {wrong # args: flock [-read|-write] [-nowait] handle [start] [length] [origin]}
  44.  
  45. Test flock-1.1 {flock argument checking} {
  46.     flock
  47. } 1 $flockWrongArgs
  48.  
  49. Test flock-1.2 {flock argument checking} {
  50.     flock $readFH 0 0 0 0
  51. } 1 $flockWrongArgs
  52.  
  53. Test flock-1.3 {flock argument checking} {
  54.     flock -writx $readFH
  55. } 1 {invalid option "-writx" expected one of "-read", "-write", or "-nowait"}
  56.  
  57. Test flock-1.4 {flock argument checking} {
  58.     flock -nowait
  59. } 1 $flockWrongArgs
  60.  
  61. Test flock-1.5 {flock argument checking} {
  62.     flock foofile
  63. } 1 {bad file identifier "foofile"}
  64.  
  65. Test flock-1.6 {flock argument checking} {
  66.     flock $readFH x
  67. } 1 {expected integer but got "x"}
  68.  
  69. Test flock-1.7 {flock argument checking} {
  70.     flock $readFH 1 x
  71. } 1 {expected integer but got "x"}
  72.  
  73. Test flock-1.8 {flock argument checking} {
  74.     flock $readFH {} x
  75. } 1 {expected integer but got "x"}
  76.  
  77. Test flock-1.9 {flock argument checking} {
  78.     flock $readFH {} 1 bad
  79. } 1 {bad origin "bad": should be "start", "current", or "end"}
  80.  
  81. Test flock-1.10 {flock argument checking} {
  82.     flock $readFH
  83. } 1 {file not open for writing}
  84.  
  85. Test flock-1.11 {flock argument checking} {
  86.     flock -write $readFH 
  87. } 1 {file not open for writing}
  88.  
  89. Test flock-1.12 {flock argument checking} {
  90.     flock -read $writeFH 
  91. } 1 {file not open for reading}
  92.  
  93. Test flock-1.13 {flock argument checking} {
  94.     flock -read -write $rdwrFH 
  95. } 1 {can not specify both "-read" and "-write"}
  96.  
  97.  
  98. #
  99. # Check funlock argument checking
  100. #
  101.  
  102. set funlockWrongArgs {wrong # args: funlock handle [start] [length] [origin]}
  103.  
  104. Test flock-2.1 {funlock argument checking} {
  105.     funlock
  106. } 1 $funlockWrongArgs
  107.  
  108. Test flock-2.2 {funlock argument checking} {
  109.     funlock $readFH 0 0 0 0
  110. } 1 $funlockWrongArgs
  111.  
  112. Test flock-2.3 {funlock argument checking} {
  113.     funlock -write $readFH
  114. } 1  {bad file identifier "-write"}
  115.  
  116. Test flock-2.4 {funlock argument checking} {
  117.     funlock foofile
  118. } 1 {bad file identifier "foofile"}
  119.  
  120. Test flock-2.5 {funlock argument checking} {
  121.     funlock $readFH x
  122. } 1 {expected integer but got "x"}
  123.  
  124. Test flock-2.6 {funlock argument checking} {
  125.     funlock $readFH 1 x
  126. } 1 {expected integer but got "x"}
  127.  
  128. Test flock-2.7 {funlock argument checking} {
  129.     funlock $readFH {} x
  130. } 1 {expected integer but got "x"}
  131.  
  132. Test flock-2.8 {funlock argument checking} {
  133.     funlock $readFH {} 1 bad
  134. } 1 {bad origin "bad": should be "start", "current", or "end"}
  135.  
  136. #
  137. # If problems with acquiring locks, bail out now, as some tests may hang.
  138. #
  139. if {[catch {flock $writeFH} msg] != 0} {
  140.     puts stderr "*************************************************************"
  141.     puts stderr "Error acquiring file lock, may be system configuration"
  142.     puts stderr "rest of lock tests skipped"
  143.     puts stderr "    $msg"
  144.     puts stderr "*************************************************************"
  145.     catch {close $writeFH}
  146.     catch {close $readFH}
  147.     catch {close $rdwrFH}
  148.     unlink -nocomplain {FLOCKR.TMP FLOCKRW.TMP FLOCKW.TMP}
  149.     return
  150. }
  151. funlock $writeFH
  152.  
  153.  
  154. #
  155. # Check locking of a file that is not locked
  156. #
  157.  
  158. Test flock-3.1 {flock/unlock of a file that is not locked} {
  159.    flock $writeFH
  160.    funlock $writeFH
  161. } 0 {}
  162.  
  163. Test flock-3.2 {flock/unlock of a file that is not locked} {
  164.    flock -write $writeFH
  165.    funlock $writeFH
  166. } 0 {}
  167.  
  168. Test flock-3.3 {flock/unlock of a file that is not locked} {
  169.    flock -write $rdwrFH
  170.    funlock $rdwrFH
  171. } 0 {}
  172.  
  173. Test flock-3.2 {flock/unlock of a file that is not locked} {
  174.    flock -read $readFH
  175.    funlock $readFH
  176. } 0 {}
  177.  
  178. #
  179. # Start a process to lock a file.  A pipe will be used to report when its
  180. # locked.
  181.  
  182. pipe fromChild toParent
  183. flush stdout
  184. flush stderr
  185. set lockerPid [fork]
  186. if {$lockerPid == 0} {
  187.    flock $writeFH
  188.    flock $rdwrFH 0 10
  189.    puts $toParent "*I am ready*"
  190.    flush $toParent
  191.    while 1 {sleep 20}
  192.    exit 0
  193. }
  194. if {([gets $fromChild line] < 0) || ([set line] != "*I am ready*")} {
  195.    error "Unexpected response from flock test child: $line"}
  196.  
  197. Test flock-4.1 {flock of file locked by child process} {
  198.    flock -nowait $writeFH
  199. } 0 0
  200.  
  201. Test flock-4.2 {flock of file locked by child process} {
  202.    flock -nowait $rdwrFH 0 5
  203. } 0 0
  204.  
  205. Test flock-4.3 {flock of file locked by child process} {
  206.    flock -nowait $rdwrFH 0 5 start
  207. } 0 0
  208.  
  209. set rdwrSize [fstat $rdwrFH size]
  210.  
  211. Test flock-4.4 {flock of file locked by child process} {
  212.    flock -nowait $rdwrFH -$rdwrSize 5 end
  213. } 0 0
  214.  
  215. Test flock-4.4 {flock of file locked by child process} {
  216.    set stat [flock -nowait $rdwrFH 10 12 start]
  217.    funlock $rdwrFH 10 12 start
  218.    set stat
  219. } 0 1
  220.  
  221. kill $lockerPid
  222. wait $lockerPid
  223.  
  224. catch {close $readFH}
  225. catch {close $writeFH}
  226. catch {close $rdwrFH}
  227. catch {close $fromChild}
  228. catch {close $toParent}
  229. unlink -nocomplain {FLOCKR.TMP FLOCKRW.TMP FLOCKW.TMP}
  230.